﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using VitalShining.WebControls;

namespace CARMS.SysPage
{
    [Function("SysPage.PageList")]
    public partial class PageList :PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNewItem.Enabled = true;
                PageQueryAdapter1.PerformQuery();
            }
        }


        protected override void btnNewItem_Click(object sender, EventArgs e)
        {
            RediretResolveUrl(SiteUrls.SysPage.PageCreate);
        }


        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            PredicateExpression Condition = new PredicateExpression();
            Condition.Add(controlCondition.GetPredicateExpression());
            Condition.Add(ActionFields.ParentId != string.Empty);
            Condition.Add(ActionFields.IsActive == true);
            e.Condition = Condition;
        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            var collection = new ActionCollection();
            e.RecordCount = collection.GetDbCount((PredicateExpression)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            SortExpression sort;
            switch (gridList.SortExpression)
            {
 
                case "ActionID":
                    sort = new SortExpression(ActionFields.ActionId | (SortOperator)(int)gridList.SortDirection);
                    break;
                case "ActionName":
                    sort = new SortExpression(ActionFields.ActionName | (SortOperator)(int)gridList.SortDirection);
                    break;
                case "ParentID":
                    sort = new SortExpression(ActionFields.ParentId | (SortOperator)(int)gridList.SortDirection);
                    break;

                case "SortIndex":
                    sort = new SortExpression(ActionFields.SortIndex | (SortOperator)(int)gridList.SortDirection);
                    break;
                case "CreateDate":
                    sort = new SortExpression(ActionFields.CreateDate | (SortOperator)(int)gridList.SortDirection);
                    break;
                case "DBTableName":
                    sort = new SortExpression(ActionFields.DbtableName | (SortOperator)(int)gridList.SortDirection);
                    break;
                    
                default:
                    sort = new SortExpression(ActionFields.ParentId | SortOperator.Ascending);
                    break;
            }

            var collection = new ActionCollection();
            collection.GetMulti((PredicateExpression)e.Condition, 0, sort, null, null, e.PageIndex + 1, e.PageSize);
            e.DataSource = collection;

        }

        protected void gridList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //var data = (ActionEntity)e.Row.DataItem;
                //var rptRoles = (Repeater)e.Row.FindControl("rptRoles");
                //if (rptRoles != null)
                //{
                //    var roles = SecurityBusiness.GetRoleListByActionID(data.ActionId);

                //    rptRoles.DataSource = roles;
                //    rptRoles.DataBind();
                //}
                //e.Row.Attributes["ActionId"] = data.ActionId.ToString();
            }
        }

        protected void gridList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "ViewItem":
                    RediretResolveUrl(SiteUrls.SysPage.PageDetail + "?ActionID=" + e.CommandArgument);
                    break;
                case "ConfigGrid":
                    RediretResolveUrl(SiteUrls.SysPage.PageGridConfig + "?ActionID=" + e.CommandArgument);
                    break;

                    
            }
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }

        protected void btnViewAll_Click(object sender, EventArgs e)
        {
            controlCondition.InitalControls("SysPage.PageList", "Action");
            PageQueryAdapter1.PerformQuery();
        }
    }
}
